import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/page/login/kuangdeng/kuang_deng_login2_page.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';
import 'package:get/get.dart';

import '../../../net/dio_utils.dart';
import '../../../utils/sp_utils.dart';
import '../../login/LoginType.dart';

class UpdateassowrdController extends GetxController {
  bool isSubmit = false;

  ///[originPassword]原密码
  ///[newPassword] 新密码
  Future<bool> submitAlertUserPassword(
      String originPassword, String newPassword) async {
    isSubmit = true;
    update();
    Map<String, dynamic> map2 = {};
    map2["oldPassword"] = originPassword;
    map2["password"] = newPassword;
    //当前的登录方式
    String loginType = SPUtil.getString("loginType");
    if (loginType != null && loginType != "") {
      if (loginType == LoginType.staff.toString()) {
        map2["loginType"] = 2;
      } else {
        map2["loginType"] = 1;
      }
    } else {
      map2["loginType"] = 1;
    }

    ResponseInfo responseInfo = await DioUtils.instance
        .postRequest(url: HttpHelper.userInfoUpdatPassword, jsonMap: map2);
    if (responseInfo.success) {
      //解析数据
      ToastUtils.showToast("修改成功 请重新登录");
      Future.delayed(Duration(milliseconds: 1000), () {
        UserHelper.getInstance.exitLogin();
        //关闭首页面
        // rootStreamController.add(GlobalBean(202, "关闭"));
        // NavigatorUtils.pop(context);
        Get.offAll(() => KuangDengLogin2Page());
      });
    } else {
      ToastUtils.showToast(responseInfo.message);
    }
    isSubmit = false;
    update();
    return true;
  }
}
